home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 223_01 / itou.c < prev    next >
Text File  |  1980-01-01  |  896b  |  25 lines

  1. #include stdio.h
  2. /*
  3. ** itou -- convert nbr to unsigned decimal string of width sz
  4. **         right adjusted, blank filled; returns str
  5. **
  6. **        if sz > 0 terminate with null byte
  7. **        if sz = 0 find end of string
  8. **        if sz < 0 use last byte for data
  9. */
  10.    static int lowbit;
  11.  
  12. itou(nbr, str, sz)  int nbr;  char str[];  int sz;  {
  13.   if(sz>0) str[--sz]=NULL;
  14.   else if(sz<0) sz = -sz;
  15.   else while(str[sz]!=NULL) ++sz;
  16.   while(sz) {
  17.     lowbit=nbr&1;
  18.     nbr=(nbr>>1)&32767;  /* divide by 2 */
  19.     str[--sz]=((nbr%5)<<1)+lowbit+'0';
  20.     if((nbr=nbr/5)==0) break;
  21.     }
  22.   while(sz) str[--sz]=' ';
  23.   return str;
  24.   }
  25.